<?

if(isset($_POST["insert_meeting"]))
    if(($value = $Meetings->insert($_POST["group"],$_POST["date"],$_POST["start_time"],$_POST["end_time"],$_POST["location"])) === true)
        header("Location: /?page=supervised");
    else
        $error .= $value;

if(isset($_POST["update_meeting"]))
    if(($value = $Meetings->update($_POST["id"],$_POST["date"],$_POST["start_time"],$_POST["end_time"],$_POST["location"])) === true)
        header("Location: /?page=supervised");
    else
        $error .= $value;

if(isset($_POST["delete_meeting"]))
    if(($value = $Meetings->delete($_POST["id"])) === true)
        header("Location: /?page=supervised");
    else
        $error .= $value;

?>
<div id="content">
    <h2>Valvomani ryhmät</h2>
    <? echo $error; ?>
    <ul>
        <? $superviseds = $Groups->get_by_superviser($user);
        foreach($superviseds as &$supervised) { ?>
            <li>
                <h3><a href="javascript:void(0);" onclick="toggle(this)" class="toggle"><? echo e($supervised->row_number); ?>. <? echo e($supervised->course); ?></a></h3>
                <ul>
                    <li>
                        <h4>Tapaamiset</h4>
                        <table class="timetable">
                            <thead>
                                <tr>
                                    <th class="number">#</th>
                                    <th class="date">Päivä</th>
                                    <th class="start_time">Alkaa</th>
                                    <th class="end_time">Päättyy</th>
                                    <th class="location">Paikka</th>
                                    <th class="reset"></th>
                                    <th class="update"></th>
                                    <th class="delete"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <? $meetings = $Meetings->get_by_group($supervised->id);
                                $i = 0;
                                $date = null;
                                $start_time = null;
                                $end_time = null;
                                $location = null;
                                foreach($meetings as &$meeting)
                                {
                                    ++$i;
                                    $id = e($meeting->id);
                                    $date = date('d.m.Y',$meeting->date);
                                    $start_time = date('H:i',$meeting->start_time);
                                    $end_time = date('H:i',$meeting->end_time);
                                    $location = e($meeting->location);
                                    ?>
                                    <tr>
                                        <form method="post" action="?page=supervised">
                                            <input type="hidden" name="id" value="<? echo $id; ?>" />
                                            <td class="number"><? echo $i; ?></td>
                                            <td class="date"><input type="text" name="date" maxlength="10" value="<? echo $date; ?>" /></td>
                                            <td class="start_time"><input type="text" name="start_time" maxlength="5" value="<? echo $start_time ?>" /></td>
                                            <td class="end_time"><input type="text" name="end_time" maxlength="5" value="<? echo $end_time; ?>" /></td>
                                            <td class="location"><input type="text" name="location" value="<? echo $location; ?>" /></td>
                                            <td class="reset"><input type="reset" name="reset" value="Kumoa" /></td>
                                            <td class="update"><input type="submit" name="update_meeting" value="Muokkaa" /></td>
                                            <td class="delete"><input type="submit" name="delete_meeting" value="Poista" /></td>
                                        </form>
                                    </tr>
                                <? } if($date != null) $date = date('d.m.Y',strtotime($date) + 7 * 24 * 60 * 60); ?>
                                <tr>
                                    <form method="post" action="?page=supervised">
                                        <input type="hidden" name="group" value="<? echo e($supervised->id); ?>" />
                                        <td class="number"></td>
                                        <td class="date"><input type="text" name="date" maxlength="10" value="<? echo $date; ?>" /></td>
                                        <td class="start_time"><input type="text" name="start_time" maxlength="5" value="<? echo $start_time ?>" /></td>
                                        <td class="end_time"><input type="text" name="end_time" maxlength="5" value="<? echo $end_time; ?>" /></td>
                                        <td class="location"><input type="text" name="location" value="<? echo $location; ?>" /></td>
                                        <td class="reset"><input type="reset" name="reset" value="Kumoa" /></td>
                                        <td class="update"><input type="submit" name="insert_meeting" value="Lisää" /></td>
                                        <td class="delete"></td>
                                    </form>
                                </tr>
                            </tbody>
                        </table>
                    </li>
                    <li>
                        <h4>Opiskelijat</h4>
                        <table class="students">
                            <thead>
                                <tr>
                                    <th class="number">#</th>
                                    <th class="name">Nimi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <? $students = $Groups->get_members($supervised->id); $i = 0;
                                foreach($students as &$student) { ++$i; ?>
                                    <tr>
                                        <td class="number"><? echo $i; ?></td>
                                        <td class="name"><? echo e($student->name); ?></td>
                                    </tr>
                                <? } ?>
                            </tbody>
                        </table>
                    </li>
                </ul>
            </li>
        <? } ?>
    </ul>
</div>
